# PPT unit 13 function I
# page 52

from python_baway.common.xcommon import sep


def xin100div7div():
    '''定义函数实现如下功能：
输出1-100之间所有能被7整除但是不能被5整除的数'''
    xlist = []
    for i in range(1, 100 + 1):
        if 0 == i % 7 and i % 5:
            xlist.append(i)
    return xlist


sep()
print(xin100div7div())


def xinput_numbers():
    '''定义函数实现如下功能：
不断地输入数字，输入b退出，将输入的所有数字返回
调用该函数，然后打印出以下内容：总和是多少  有多少个  平均是多少
'''
    xlist = []
    while True:
        ch = input('Integer? ("b" for quit): ').strip()
        if 'b' == ch:
            break
        xlist.append(int(ch))
    xlen = len(xlist)
    if 0 == xlen:
        print('No integers at all!')
        return
    xsum = 0
    for i in xlist:
        xsum += i
    xeva = xsum / xlen
    print('''\
Sum: {},
Count: {},
Average: {}
'''.format(xsum, xlen, xeva))
    return xlist


sep(label='B for interrupt:')
xinputs = xinput_numbers()
print('Inputed numbers:', xinputs)


def is_empty(x):
    '''写一个函数，判断用户传入的对象（字符串、列表、元组）的元素是否为空
'''
    xtype = type(x)
    if xtype == str:
        return '' == x
    elif xtype == list:
        return 0 == len(x)
    elif xtype == tuple:
        return 0 == len(x)
    else:
        return not(x)


sep()
for i in ('', 'abc', [1, 2, 3], [], tuple(), (1,), (1, 2, 3), 0, 123, False, True):
    print(repr(i), is_empty(i))


def x_stat(xstr):
    '''写函数，统计字符串中有几个字母，几个数字，几个空格，几个其他字符，并返回结果
'''
    xletter = xdigit = xspace = xother = 0
    for a in xstr:
        if a.isalpha():
            xletter += 1
        elif a.isdigit():
            xdigit += 1
        elif a.isspace():
            xspace += 1
        else:
            xother += 1
    print('''\
Letter: {},
Digit: {},
Space: {},
Other: {}
    '''.format(xletter, xdigit, xspace, xother))


sep()
import string
x_stat(string.printable)
